package me.ezitku.base.extensions.Crypt

import android.media.MediaCodec
import java.math.BigInteger
import java.security.MessageDigest
import java.security.NoSuchAlgorithmException
import android.text.TextUtils
import kotlin.experimental.and


/**
 * Created by yurqi on 2018/3/11.
 * QQ: 906120575 | Wechat : kalip008
 * Tashkin Company
 */

fun md5(msg: String?): String {

    if (msg == null || msg.isEmpty()) {
        throw NullPointerException("md5 (msg cannot must be null)")
    }

    val messageDigest = MessageDigest.getInstance("MD5")
    //val md5 = messageDigest.digest(msg.toByteArray())
    messageDigest.update(msg.toByteArray())
    var result = msg!!

    val hash = BigInteger(1, messageDigest.digest())
    result = hash.toString(16)
    while (result.length < 32) { //40 for SHA-1
        result = "0" + result
    }

    return result
}

fun md51(string: String): String {
    if (TextUtils.isEmpty(string)) {
        return ""
    }
    var md5: MessageDigest? = null
    try {
        md5 = MessageDigest.getInstance("MD5")
        val bytes = md5!!.digest(string.toByteArray())
        var result = ""
        for (b in bytes) {
            var temp = Integer.toHexString((b and 0xff.toByte()).toInt())
            if (temp.length == 1) {
                temp = "0" + temp
            }
            result += temp
        }
        return result
    } catch (e: NoSuchAlgorithmException) {
        e.printStackTrace()
    }

    return ""
}